app.push(function(){
    var Comments = function(){
        this.showComments = function(data){
        	app.main.getTemplate(function(){
        		delete data.data.log;
        		var ul = $.tmpl('comments_block',{});
        		ul.addClass('comments');
	            for(var i  in data.data){
	                parsetree(ul,data.data[i]);
	            }
	            app.elements.comments.html('').append(ul);
        	});
        }
        var parsetree = function(el,tree){
            var this_ = this;
            var li = $.tmpl('comment_element',tree)
            li.find('i').click(function(){
                app.comments.delComment(tree.comment_id);
            });
            li.find('input[type="button"]').click(function(){
                app.comments.addComment(tree.comment_id,
                {
                    comment_id:6345,
                    user_id:1,
                    user_name:2,
                    description:$(this).parent().find('textarea').attr('value')
                });

                $(this).parent().hide();
                $(this).parent().parent().find(".req:first").show()
            });
            li.find('.req').click(function(){
            	$(this).hide();
            	$(this).parent().find(".comment_reply_block:first").show()
            });
            if(tree.child){
                for(var i in tree.child){
                    parsetree(li.find('.unstyled:first'),tree.child[i]);
                }
            }
            el.append(li);
        }
        var deleteFromTree = function(id){
            var li = $('.comment[rel='+id+']');
            if(!li.length) return;
            var ul = li.find('ul:first li');
            if(ul.length > 0){
                li.find('p:first i, span.req:first, div:first').remove();
                li.find('span:first ').remove();
                li.find('p:first > span').text('Удалено');
            }else li.remove();

        }
        this.delComment = function(id){
            mlhAPI.article.delComment({
                id: id,
                callback: function(result){
                    if(result['result'] == 'success'){
                        if(result.data['error'])alert(result.data['error']);
                        else deleteFromTree(id);
                    }
                }
            });
        }
        this.addComment = function(id,data){
            if(id){
                var li = $('.comment[rel='+id+']');
                if(!li.length) return;
                var ul = li.find('ul:first');
                if(!ul.length) {
                    ul = $.tmpl('comments_block',{});
                    li.append(ul);
                }
            } else {
                ul = $('.comments');
            }
            mlhAPI.article.setComment({
                parent_id: id,
                model_id: 1,///костыль
                description: data.description,
                callback: function(result){
                    if(result['result'] == 'success'){
                        if(result.data['error'])alert(result.data['error']);
                        else parsetree(ul,data);
                    }
                }
            });
        }
    }
    app['comments'] = new Comments();
});